#include "render_line.h"
#include "qbe/src/layout/layout.hpp"

namespace qbe::utils::TempRender {

RenderLine::RenderLine(vsg::ref_ptr<vsg::Options> options, std::vector<qlib::Path::s_ptr> lines) : m_lines(lines) {
  switchObject = vsg::Switch::create();
  transform = vsg::MatrixTransform::create();
  switchObject->addChild(true, transform);

  std::vector<Mesh2D> meshs;
  for (auto i : m_lines) {
    auto mesh = Mesh2D(i->spine.points, false);
    meshs.push_back(mesh);
  }
  Mesh2D merge_result;
  mesh_merge(meshs, merge_result);
  LineShaderMaterial2D material{Line_Uniform{vsg::vec4(1.0, 0.0, 0.0, 1.0), vsg::vec2(1316.0, 847.0), 100.0, 70.0, true},
                                "shaders/cell/line.vert.spv", "shaders/cell/line.frag.spv"};

  line_node = LineMeshNode::create(options, qlib::Tag{0, 0}, merge_result, material, VkPrimitiveTopology::VK_PRIMITIVE_TOPOLOGY_LINE_LIST);
  transform->addChild(line_node);
}

RenderLine ::~RenderLine() {}

void RenderLine::setLayerType(vsg::vec4 color, std::string line_type) {
  line_node->setLayerType(color, line_type);
}

void RenderLine::updateData(std::vector<qlib::Path::s_ptr> lines) {
  if (lines.empty())
    return;
  m_lines = lines;
  std::vector<Mesh2D> meshs;
  for (auto i : m_lines) {
    auto mesh = Mesh2D(i->spine.points, false);
    meshs.push_back(mesh);
  }
  Mesh2D merge_result;
  mesh_merge(meshs, merge_result);
  line_node->mesh = merge_result;
  line_node->update();
}

void RenderLine::setShowHide(bool is_show) {
  switchObject->setAllChildren(is_show);
}

}  // namespace qbe::utils::TempRender